package com.bstech.Classi;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import com.bstech.Utils.Files;

import android.util.Log;

public class ClassSettings implements Serializable {

	private static final long serialVersionUID = 19012L;

	/***** SETTINGS ******/
	public boolean useLog,RememberLogin;
	public String UserName;
	public String Password;
	public int LastUserId;
	public int PhotoCompression, SyncIntervalMs, GPSLocalizationTimeoutMinutes;
	//TODO: cambiare anche nella SettingsActivity.GetTempSettings()
	public long LastSyncDateTecnici,LastSyncDateClienti,
			   LastSyncDateDestinazioni,LastSyncDateImpianti,
			   LastSyncDateChiamate,LastSyncDateRapportiniTecnici,
			   LastSyncDateRapportiniDettagli,LastSyncDateRapportini; 
	
	public double LastLongitude,LastLatitude;

	/***** METODI ******/
	public static boolean Serialize(ClassSettings obj,String filepath) {
		byte[] buff = SerializeInternal(obj);
		if (buff == null)
			return false;
		return Files.WriteFile(buff, filepath);
	}
	private static byte[] SerializeInternal(Object o) {
		ByteArrayOutputStream bos = new ByteArrayOutputStream();

		try {
			ObjectOutput out = new ObjectOutputStream(bos);
			out.writeObject(o);
			out.close();

			// Get the bytes of the serialized object
			byte[] buf = bos.toByteArray();

			return buf;
		} catch (IOException ioe) {
			Log.e("serializeObject", "error", ioe);

			return null;
		}
	}
	public static ClassSettings Deserialize(String filepath) {
		ClassSettings ret = null;
		
		byte[] buff = Files.ReadFile(filepath);
		if (buff != null)
			ret =  DeserializeInternal(buff);
		
		if(ret == null)
		{
			//ritorno i settaggi di default e li serializzo
			ret  = DefaultSettings();
			ClassSettings.Serialize(ret, filepath);
		}
		
		return ret;
	}
	private static ClassSettings DeserializeInternal(byte[] b) {
		try {
			ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
			Object object = in.readObject();
			in.close();

			return (ClassSettings) object;
		} catch (ClassNotFoundException cnfe) {
			Log.e("deserializeObject", "class not found error", cnfe);
			return null;
		} catch (IOException ioe) {
			Log.e("deserializeObject", "io error", ioe);
			return null;
		}
	}
	private static ClassSettings DefaultSettings()
	{
		ClassSettings def = new ClassSettings();
		def.useLog = true;
		def.RememberLogin = false;
		def.UserName = "";
		def.Password = "";
		def.PhotoCompression = 2;
		def.SyncIntervalMs = 3 * 3600000;
		
		def.LastSyncDateTecnici = 19000101000000L;
		def.LastSyncDateClienti = 19000101000000L;
		def.LastSyncDateDestinazioni = 19000101000000L;
		def.LastSyncDateImpianti = 19000101000000L;
		def.LastSyncDateChiamate = 19000101000000L;
		def.LastSyncDateRapportiniTecnici = 19000101000000L;
		def.LastSyncDateRapportiniDettagli = 19000101000000L; 
		def.LastSyncDateRapportini = 19000101000000L;
		
		def.GPSLocalizationTimeoutMinutes = 2;
		
		def.LastLongitude = 0;
		def.LastLatitude = 0;
		
		def.LastUserId = 0;
		
		return def;
	}
	
}
